package kr.co.waitnumber.site.management.dao;

import kr.co.waitnumber.site.domain.Member;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import java.util.List;

public class MemberDaoImpl extends SqlMapClientDaoSupport implements MemberDao {

    // SLF4J Logging
    private Logger logger = LoggerFactory.getLogger(MemberDaoImpl.class);


    public List getMemberListBySearchword(Member member){

        /**
         * Null 에러 방지용 공백 전환
         */
//        if(member.getId() == null){
//            member.setId("");
//        }
//
//        if(member.getNick_name() == null){
//            member.setNick_name("");
//        }
//
//        if(member.getEmail() == null){
//            member.setEmail("");
//        }

        return getSqlMapClientTemplate().queryForList("MEMBER.getMemberListBySearchword", member, member.getCurrentPage() * member.getPageSize(), member.getPageSize());
    }

    public String getTotalMemberCountBySearchword(Member member){
        return (String)getSqlMapClientTemplate().queryForObject("MEMBER.getTotalMemberCountBySearchword", member);
    }

    /**
     * 트랜잭션 TEST 1
     *
     * @param member
     * @return
     */
    public String transactionInsertTest(Member member){
        return Integer.toString(getSqlMapClientTemplate().update("MEMBER.transactionInsertTest", member));
    }

    /**
     * 트랜잭션 TEST 2
     *
     * @param member
     * @return
     */
    public String transactionUpdateTest(Member member){
        return Integer.toString(getSqlMapClientTemplate().update("MEMBER.transactionUpdateTest", member));
    }

    /**
     * 트랜잭션 TEST 3
     *
     * @param id
     * @return
     */
    public String transactionDeleteTest(String id){
        return Integer.toString(getSqlMapClientTemplate().delete("MEMBER.transactionDeleteTest", id));
    }

    //Transaction Test
    public void transactionVoidInsertTest(Member member){
        Integer.toString(getSqlMapClientTemplate().update("MEMBER.transactionInsertTest", member));
    }
    public void transactionVoidUpdateTest(Member member){
        Integer.toString(getSqlMapClientTemplate().update("MEMBER.transactionUpdateTest", member));
    }
    public void transactionVoidDeleteTest(String id){
        Integer.toString(getSqlMapClientTemplate().delete("MEMBER.transactionDeleteTest", id));
    }
    /////////////////////////

    public Member getMemberById(String id) {
        return (Member)getSqlMapClientTemplate().queryForObject("MEMBER.getMemberById", id);
    }

    public Member getMemberByEmail(String id){
        return (Member)getSqlMapClientTemplate().queryForObject("MEMBER.getMemberByEmail", id);
    }

    public Member getMemberByIdEmail(Member member){
        return (Member)getSqlMapClientTemplate().queryForObject("MEMBER.getMemberByIdEmail", member);
    }




    public String updateMemberById(Member member){
        return Integer.toString(getSqlMapClientTemplate().update("MEMBER.updateMemberById", member));
    }

    public String adminUpdateMemberById(Member member){
        return Integer.toString(getSqlMapClientTemplate().update("MEMBER.adminUpdateMemberById", member));
    }

    public String updateMemberPasswordById(Member member){
        return Integer.toString(getSqlMapClientTemplate().update("MEMBER.updateMemberPasswordById", member));
    }

    public String getMemberExistById(String id) {
        return (String)getSqlMapClientTemplate().queryForObject("MEMBER.getMemberExistById", id);
    }

    public String getMemberExistByEmail(String email){
        return (String)getSqlMapClientTemplate().queryForObject("MEMBER.getMemberExistByEmail", email);
    }

    public String getMemberExistByIdEmail(Member member){
        return (String)getSqlMapClientTemplate().queryForObject("MEMBER.getMemberExistByIdEmail", member);
    }


    public String deleteMemberById(String id){
        return Integer.toString(getSqlMapClientTemplate().delete("MEMBER.deleteMemberById", id));
    }

    public String insertMember(Member member){
        return Integer.toString(getSqlMapClientTemplate().update("MEMBER.insertMember", member));
    }
}
